package com.huluobo.a2205parkdemo.sign

import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2205parkdemo.adapter.SignAdapter
import com.huluobo.a2205parkdemo.databinding.ActivitySignMonthBinding
import com.huluobo.base.bean.SignMonthBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class SignMonthActivity : BaseActivity<ActivitySignMonthBinding, SignViewModel>() {
    private var month = 1
    private lateinit var signAdapter: SignAdapter
    override fun initData() {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_MONTH] = month
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        vm.getMonthSign(body)

        vm.getMonthSignSuccess.observe(this) {
            val list = Gson().fromJson(it, SignMonthBean::class.java)
            signAdapter.data.clear()
            signAdapter.data.addAll(list)
            signAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.signMonthHeader.headerBack.visibility = View.VISIBLE
        binding.signMonthHeader.headerBack.setOnClickListener { finish() }
        binding.signMonthHeader.headerTitle.text = "月度考勤查询"
        binding.signMonthRv.layoutManager = LinearLayoutManager(this)
        signAdapter = SignAdapter()
        binding.signMonthRv.adapter = signAdapter

        binding.signMonthTv.text = "${month}月"
        binding.signMonthPreBtn.setOnClickListener {
            if (month > 1) {
                month--
                binding.signMonthTv.text = "${month}月"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_MONTH] = month
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.getMonthSign(body)
            } else {
                ToastUtils.showLong("已经是一月了")
            }
        }

        binding.signMonthNextBtn.setOnClickListener {
            if (month >= 12) {
                ToastUtils.showLong("已经是十二月了")
            } else {
                month++
                binding.signMonthTv.text = "${month}月"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_MONTH] = month
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.getMonthSign(body)
            }
        }
    }
}